home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / VMRMix / SourceInfo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  7.8 KB  |  294 lines

  1. //------------------------------------------------------------------------------
  2. // File: SourceInfo.cpp
  3. //
  4. // Desc: DirectShow sample code
  5. //       Implementation of CMediaList, play-list of media files
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "stdafx.h"
  11. #include "SourceInfo.h"
  12.  
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16.  
  17. CMediaList::CMediaList() :
  18.                 m_avgDuration(0L),
  19.                 m_N(0),
  20.                 m_ppSI(NULL)
  21. {
  22. }
  23.  
  24. CMediaList::~CMediaList()
  25. {
  26.     Clean();
  27. }
  28.  
  29. //------------------------------------------------------------------------------
  30. void CMediaList::Clean()
  31. {
  32.     for( int i=0; i<m_N; i++)
  33.     {
  34.         delete m_ppSI[i];
  35.         m_ppSI[i] = NULL;
  36.     }// for
  37.  
  38.     delete[] m_ppSI;
  39.     m_ppSI = NULL;
  40.     m_N = 0;
  41. }
  42.  
  43.  
  44. //------------------------------------------------------------------------------
  45. // CMediaList::Add
  46. // Desc: adds a new source to the media list
  47. // return: true if success, false otherwise
  48. //------------------------------------------------------------------------------
  49. bool CMediaList::Add( SourceInfo * pSI)
  50. {
  51.     SourceInfo ** ppSInew = NULL;
  52.  
  53.     ppSInew = new SourceInfo*[m_N+1];
  54.     if( !ppSInew )
  55.         return false;
  56.  
  57.     for( int i=0; i<m_N; i++)
  58.     {
  59.         ppSInew[i] = m_ppSI[i];
  60.     }// for
  61.  
  62.     ppSInew[m_N] = pSI;
  63.     delete[] m_ppSI;
  64.     m_ppSI = ppSInew;
  65.     m_N++;
  66.  
  67.     return true;
  68. }
  69.     
  70. //------------------------------------------------------------------------------
  71. // CMediaList::Shuffle()
  72. // Desc: randomly shuffles media list content to 
  73. //       have different playback settings every time
  74. //------------------------------------------------------------------------------
  75. void CMediaList::Shuffle()
  76. {
  77.     SourceInfo *pSIaux = NULL;
  78.     int n1;
  79.     int n2;
  80.  
  81.     for(int i=0; i< m_N; i++)
  82.     {
  83.         n1 = rand() * (m_N+1) / RAND_MAX;
  84.         n2 = rand() * (m_N+1) / RAND_MAX;
  85.  
  86.         n1 = ( n1<0) ? 0 : n1;
  87.         n2 = ( n2<0) ? 0 : n2;
  88.  
  89.         n1 = ( n1>m_N-1) ? m_N-1 : n1;
  90.         n2 = ( n2>m_N-1) ? m_N-1 : n2;
  91.  
  92.         if( n1 == n2 )
  93.             continue;
  94.  
  95.         pSIaux = m_ppSI[n1];
  96.         m_ppSI[n1] = m_ppSI[n2];
  97.         m_ppSI[n2] = pSIaux;
  98.     }
  99. }
  100.  
  101. //------------------------------------------------------------------------------
  102. // Name: SetInitialParameters
  103. // Purpose: calculates initial demonstration parameters for each media file --
  104. //          destination rectangles and alpha levels
  105. //          Note that these values are used as parameters in CDemonstration and 
  106. //          that changing them can cause different appearence of the demonstration          
  107. //------------------------------------------------------------------------------
  108. void CMediaList::SetInitialParameters()
  109. {
  110.     NORMALIZEDRECT rectD;
  111.     double Alpha;
  112.     int i;
  113.     // set alpha levels and destination rectangles here
  114.     for( i=0; i< m_N; i++)
  115.     {
  116.         Alpha = 0.3 + 0.68 * (double)rand() / RAND_MAX; // random in [0.3; 0.98]
  117.         
  118.         rectD.left = rectD.top = 0.1f;
  119.         rectD.right = rectD.bottom = 0.9f;
  120.  
  121.         this->GetItem(i)->m_fAlpha = Alpha;
  122.         this->GetItem(i)->m_rD = rectD;
  123.     }// for
  124. }
  125.  
  126. //------------------------------------------------------------------------------
  127. // Name: Initialize
  128. // Purpose: go through m_szMediaFolder and retrieve all media files into
  129. // Parameters: none
  130. // Return: true if folder contains at least one valid file;
  131. //         false otherewise
  132. //------------------------------------------------------------------------------
  133. bool CMediaList::Initialize(char *szFolder)
  134. {
  135.     struct _finddata_t fileinfo;
  136.     long filehandle = -1L;
  137.     int nRes;
  138.     int nCounter = 0; // to prevent loading huge number of files,
  139.                       // let's restrict it to 50
  140.     char szMask[MAX_PATH];
  141.     char szExt[] = "*.AVI;*.MOV;*.MPG;*.MPEG;*.VOB;*.QT;";
  142.     char szCurExt[MAX_PATH];
  143.     char szFilePath[MAX_PATH];
  144.     char *psz = NULL;
  145.  
  146.     // clean the list
  147.     Clean();
  148.  
  149.     if( !_strcmpi(szFolder,""))
  150.         return false;
  151.  
  152.     do
  153.     {
  154.         strcpy(szCurExt,szExt);
  155.         psz = strstr(szCurExt,";");
  156.         if( psz)
  157.         {
  158.             *psz = 0;
  159.             psz = NULL;
  160.             psz = strstr( szExt, ";");
  161.             if( psz )
  162.             {
  163.                 strcpy( szExt, psz+1);
  164.             }
  165.         }
  166.         else
  167.         {
  168.             strcpy( szExt, "");
  169.         }
  170.         sprintf(szMask, "%s%s", szFolder, szCurExt);
  171.  
  172.         filehandle = _findfirst(szMask, &fileinfo);
  173.  
  174.         if( filehandle == -1L)
  175.             continue;
  176.         
  177.         SourceInfo * pSI = NULL;
  178.         pSI = new SourceInfo;
  179.         if( !pSI )
  180.         {
  181.             return false;
  182.         }
  183.         sprintf( szFilePath, "%s%s", szFolder, fileinfo.name);
  184.         strcpy( pSI->m_szPath, (const char*)szFilePath);
  185.         MultiByteToWideChar(CP_ACP, 0, (const char*)szFilePath, -1, pSI->m_wszPath, _MAX_PATH); 
  186.         
  187.         Add( pSI );
  188.         nCounter++;
  189.  
  190.         nRes = _findnext(filehandle, &fileinfo);
  191.  
  192.         while( -1L != nRes )
  193.         {
  194.             pSI = NULL;
  195.             pSI = new SourceInfo;
  196.             if( !pSI )
  197.             {
  198.                 return false;
  199.             }
  200.             sprintf( szFilePath, "%s%s", szFolder,fileinfo.name);
  201.             strcpy( pSI->m_szPath, (const char*)szFilePath);
  202.             MultiByteToWideChar(CP_ACP, 0, (const char*)szFilePath, -1, pSI->m_wszPath, _MAX_PATH); 
  203.  
  204.             Add( pSI );
  205.             nCounter++;
  206.             nRes = _findnext(filehandle, &fileinfo); 
  207.         }// while
  208.  
  209.     } while( _strcmpi(szExt, "") && nCounter < 50 );
  210.  
  211.     if( 0 == Size() )
  212.     {
  213.         return false;
  214.     }
  215.     else
  216.     {
  217.         return true;
  218.     }
  219. }
  220.  
  221. //------------------------------------------------------------------------------
  222. // Name: Clone
  223. // Purpose: copies elements (nStart; nStart+n) to the list pML
  224. // Parameters: n      - number of elements
  225. //             pML    - cloned media list
  226. //             nStart - start position in (this) list; default: 0
  227. // Return: true if success;
  228. //         false otherewise
  229. //------------------------------------------------------------------------------
  230. bool CMediaList::Clone(int n, CMediaList *pML, int nStart /* = 0 */)
  231. {
  232.     bool bRes = true;
  233.  
  234.     if( n < 1 || nStart < 0 || nStart + n > m_N)
  235.         return false;
  236.  
  237.     pML->Clean();
  238.  
  239.     for(int i = nStart; i< nStart + n; i++)
  240.     {
  241.         SourceInfo *pSI = NULL;
  242.         
  243.         pSI = new SourceInfo;
  244.  
  245.         if( !pSI )
  246.             goto cleanup;
  247.  
  248.         bRes = bRes && GetItem(i)->CopyTo( pSI);
  249.  
  250.         if( false == bRes)
  251.         {
  252.             delete pSI;
  253.             goto cleanup;
  254.         }
  255.  
  256.         pSI->m_bInUse = false;
  257.  
  258.         bRes = bRes && pML->Add(pSI);
  259.         if( false == bRes)
  260.             goto cleanup;
  261.     }// for
  262.     return true;
  263.  
  264. cleanup:
  265.     pML->Clean();
  266.     return false;
  267. }
  268.  
  269. //------------------------------------------------------------------------------
  270. // Name: AdjustDuration
  271. // Purpose: calculates demonstration time. Here, it is duration of the longest file
  272. //          Change this function to set a fixed time, average time etc.         
  273. //------------------------------------------------------------------------------
  274. void CMediaList::AdjustDuration()
  275. {
  276.     m_avgDuration = 0L;
  277.     
  278.     if( 0 == m_N )
  279.     {
  280.         return;
  281.     }
  282.  
  283.     for( int i=0; i<m_N; i++)
  284.     {
  285.         
  286.         m_avgDuration = (this->GetItem(i)->m_llDuration > m_avgDuration) ?
  287.             this->GetItem(i)->m_llDuration :
  288.             m_avgDuration;
  289.     }// for
  290.  
  291. }
  292.  
  293.  
  294.